package me.zhujin.java.learnjava.myserver;

import me.zhujin.java.learnjava.myserver.enums.HttpMethod;
import me.zhujin.java.learnjava.myserver.util.ScriptUtil;

import javax.script.ScriptException;
import java.io.FileNotFoundException;

/**
 * Created by zhujin on 10/20/16.
 * blog:www.zhujin.me
 * email: zhujin.nova@gmail.com
 */
public abstract class MyServlet {


    public void service(MyRequest request, MyResponse response) throws Exception{
        if (request.getMethod() == HttpMethod.POST) {
            this.doPost(request,response);
        } else {
            this.doGet(request,response);
        }
    }

    protected abstract void doPost(MyRequest request, MyResponse response) throws Exception;

    protected abstract void doGet(MyRequest request, MyResponse response) throws Exception;

    protected void flush404(MyResponse response) {
        response.printLn("<html><head><title>MyServer1</title></head><body>")
                .printLn("404 页面不存在-")
                .printLn("</body></html>");
    }

    protected void flushDefault(MyResponse response) {
        response.printLn("<html><head><title>MyServer1</title></head><body>")
                .printLn("歡迎-")
                .printLn("</body></html>");
    }

    protected void flushBody(MyResponse response,String body) {
        response.printLn("<html><head><title>MyServer1</title></head><body>")
                .printLn(body)
                .printLn("</body></html>");
    }


//        public void service(MyRequest request, MyResponse response) throws Exception {
//        String url = request.getUrl();
//        if (url.endsWith("msp")||url.endsWith(".jsp")) {//js 脚本
//            try {
//                response.printLn(ScriptUtil.getScriptHttpBody(url, request.getParamsList().toArray()).toString());
//            } catch (FileNotFoundException e) {
//                flush404(response);
//            }
//        } else if (url.endsWith("mjsp")) {
//            // mjsp ---> java ---> .teach
//        } else {
//            defalutService(request,response);
//        }
//    }


}
